package com.zzptc.myk.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zzptc.myk.myapplication.R;
import com.zzptc.myk.myapplication.databinding.RecyclerviewBinding;
import com.zzptc.myk.myapplication.entity.ResultsBean;

import org.litepal.LitePal;

import java.util.List;

public class MyRvAdapter extends RecyclerView.Adapter<MyRvAdapter.ViewHolder> {//ViewHolder内部类

    //拿数据
    private final List<ResultsBean> resultsBeans;
    private final Context myContext;

    public MyRvAdapter(Context myContext) {
        resultsBeans= LitePal.findAll(ResultsBean.class);  //LitePal.findAll取数据
        this.myContext = myContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //绑定布局
        //原来的绑定方法 View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview,parent,false);
        RecyclerviewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.recyclerview, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ResultsBean result = resultsBeans.get(position);   //取数据
        holder.binding.tvTitle.setText(result.getTitle()); //设置标题
        Glide.with(myContext).load(result.getCover()).into(holder.binding.ivCover); //设置封面
    }

    @Override
    public int getItemCount() {
        return resultsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private final  RecyclerviewBinding binding;
        public ViewHolder(@NonNull RecyclerviewBinding binding) {
            super(binding.getRoot());
            this.binding=binding;
        }
    }
}
